package global

import (
	"github.com/nacos-group/nacos-sdk-go/clients"
	"github.com/nacos-group/nacos-sdk-go/clients/config_client"
	"github.com/nacos-group/nacos-sdk-go/clients/naming_client"
	"github.com/nacos-group/nacos-sdk-go/common/constant"
	"github.com/nacos-group/nacos-sdk-go/vo"
)

// nacos注册中心配置
type NacosConfigClientOption struct {
	//ConfigInput
	//"ip1,ip2,ip3"
	IpAddrs         string
	Port            int
	NaocesNameSpace string
	DataId          string
	Group           string
	NacosLogDir     string
	NacosCacheDir   string
	//NamingInput
	NamingServiceName string
	NamingServiceIP   string
	NamingOwner       string
	NamingPort        int
	//Output
	ConfigClient config_client.IConfigClient
	NamingClient naming_client.INamingClient
}

// GetNacosClientConfig 创建客户端配置
func GetNacosClientConfig() constant.ClientConfig {
	clientConfig := constant.ClientConfig{
		NamespaceId:         "926bc8a0-540d-4ff5-9365-646c48b11ae4", //we can create multiple clients with different namespaceId to support multiple namespace.When namespace is public, fill in the blank string here.
		RegionId:            "private",
		TimeoutMs:           5000,
		NotLoadCacheAtStart: true,
		LogDir:              "/tmp/nacos/log",
		CacheDir:            "/tmp/nacos/cache",
		LogLevel:            "error",
		//LogLevel:            "debug",
	}
	return clientConfig
}

// GetNacosClientConfig 至少一个ServerConfig
func GetNacosServerConfigs() []constant.ServerConfig {
	serverConfigs := []constant.ServerConfig{
		{
			IpAddr:      "nacos.topwhere.cn",
			ContextPath: "/nacos",
			Port:        80,
			Scheme:      "http",
		},
		//{
		//	IpAddr:      "console2.nacos.io",
		//	ContextPath: "/nacos",
		//	Port:        80,
		//	Scheme:      "http",
		//},
	}
	return serverConfigs
}

// GetNacosClient 创建链接
func GetNacosClient() vo.NacosClientParam {
	var clientConfig = GetNacosClientConfig()
	serverConfigs := GetNacosServerConfigs()

	NacosClientvo := vo.NacosClientParam{
		ClientConfig:  &clientConfig,
		ServerConfigs: serverConfigs,
	}
	return NacosClientvo
}

// GetNacosNamingClient 服务发现创建命名客户端
func GetNacosNamingClient() naming_client.INamingClient {
	GetNacosClient := GetNacosClient()
	namingClient, _ := clients.NewNamingClient(GetNacosClient)
	return namingClient
}

// GetNacosConfigClient 动态配置创建配置客户端
func GetNacosConfigClient() config_client.IConfigClient {
	GetNacosClient := GetNacosClient()
	configClient, _ := clients.NewConfigClient(GetNacosClient)
	return configClient

}
